<template>
    <div>
        机构名称<input type="text" v-model="querinfo.OrganName">
        特色服务<select v-model="querinfo.Sever">
            <option value="">请选择</option>
            <option value="特殊护理">特殊护理</option>
            <option value="24小时监护">24小时监护</option>

        </select>
        <input type="button" value="查询" class="btn btn-danger"@click="show">

        <input type="button" value="批量发布" class="btn btn-success" @click="updAll(1)">
        <input type="button" value="添加" class="btn btn-primary" @click="add">
    </div>
    <div>
        <table class="table table-bordered">
            <tbody>
                <tr style="background-color:blanchedalmond;">
                    <td><input type="checkbox" v-model="checked" @click="checkAll"></td>
                    <td align="center">机构名称</td>
                    <td align="center">特色服务</td>
                    <td align="center">分享</td>
                    <td align="center">收藏</td>
                    <td align="center">状态</td>
                    <td align="center">更新人</td>
                    <td align="center">最后更新时间</td>
                    <td align="center">机构封面</td>
                    <td align="center">营业时间</td>
                    <td align="center">机构地址</td>
                    <td align="center">联系电话</td>
                    <td align="center">发布时间</td>
                    <td align="center">详情</td>
                    <td>操作</td>
                </tr>
                <tr v-for="item in data.list">
                    <td><input type="checkbox" :value="item.id" v-model="ids" @change="checkOne"></td>
                    <td align="center">{{ item.organName }}</td>
                    <td align="center">{{ item.sever }}</td>
                    <td align="center">{{ item.fxnum }}</td>
                    <td align="center">{{ item.scnum }}</td>
                    <td align="center">{{ item.state == 1 ? "已发布" : "未发布" }}</td>
                    <td align="center">{{ item.updPeople }}</td>
                    <td align="center">{{ moment(item.endTime).format("YYYY-MM-DD") }}</td>
                    <td align="center"><img :src="item.photo" alt="" width="50" height="50px"></td>
                    <td align="center">{{ moment(item.createTime).format("YYYY-MM-DD") }}</td>
                    <td align="center">{{ item.organAddr }}</td>
                    <td align="center">{{ item.tel }}</td>
                    <td align="center">{{ item.fbTime == 1 ? "立即发布" : "定时发布" }}</td>
                    <td align="center">{{ item.xq }}</td>
                    <td><input type="button" class="btn btn-danger" value="删除" @click="Del(item.id)">
                        <input type="button" class="btn btn-success" value="下架发布" @click="upd(item.id,0)">
                        <input type="button" class="btn btn-primary" value="发布" @click="upd(item.id,1)">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <div>
        <span>共{{ data.totalcount }}条</span>
        <span>每页{{ querinfo.size }}条</span>
        <span>共{{ data.totalpage }}页</span>
        <span>{{ querinfo.index }}/{{ data.totalpage }}页</span>

        <input type="button" value="首页"  class="btn btn-primary"@click="page(1)">
        <input type="button" value="上一页"  class="btn btn-primary"@click="page(querinfo.index - 1)">
        <span v-for="item in data.totalpage" @click="page(item)">{{ item }}</span>
        <input type="button" value="下一页" class="btn btn-primary" @click="page(querinfo.index + 1)">
        <input type="button" value="尾页" class="btn btn-primary" @click="page(data.totalpage)">
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useStore } from '@/stores/user'
import moment from 'moment';
const a = useStore();
const route = useRoute();
const router = useRouter();

let checked: any = ref(false);
let ids: any = ref([])

const checkAll = () => {
    checked.value = !checked.value
    if(checked.value==true)
    {
        ids.value=data.list.map((item:any)=>{
            return item.id;
        })
    }
    else
    {
        ids.value=[]
    }
    
}
const add=()=>{
    router.push({
        path:'/organAdd'
    })
}
const checkOne = () => {
    if (ids.value.lenght == data.list.lenght) {
        checked.value=true;
        return;
    }
    else
    {
        checked.value=false;
        return;
    }
}
let querinfo: any = reactive({
    index: 1,
    size: 2,
    OrganName: '',
    Sever: ''
})

let data: any = reactive({
    "totalcount": "",
    "totalpage": "",
    "list": []
})
onMounted(() => {

    lists();
})
const show=()=>{
    querinfo.index=1;
    lists();
}
const lists = () => {
    axios({
        url: '/api/Organ/OrganList',
        method: 'get',
        params: querinfo
    })
        .then(res => {
            console.log(res);
            Object.assign(data, res.data)
        })
        .catch(err => {
            console.log(err)
        })
}
const Del=(id:any)=>{
    if(confirm("确定要删除吗"))
{
    axios({
        url:'/api/Organ/DelOrgan',
        method:'delete',
        params:{
            id:id
        }
    })
    .then(res=>{
        if (res.data >=0) {
                alert("删除成功");
                lists();
                return;
            } else {
                alert("删除失败");
                
                
                return;
            }
    })
}
}
const upd=(id:any,state:any)=>{
    if(confirm("确定要修改吗"))
{
    axios({
        url:'/api/Organ/Updstate',
        method:'put',
        params:{
            id:id,
            state:state
        }
    })
    .then(res=>{
        if (res.data >=0) {
                alert("修改成功");
                lists();
                return;
            } else {
                alert("修改失败");

                return;
            }
    })
}
}

const updAll=(state:any)=>{
    if(confirm("确定要批量发布吗"))
{
    axios({
        url:'/api/Organ/UpdstateAll',
        method:'put',
        params:{
            ids:ids.value.toString(),
            state:state
        }
    })
    .then(res=>{
        if (res.data >=0) {
                alert("修改成功");
                checked.value=false;
                ids.value=[]
                lists();
                return;
            } else {
                alert("修改失败");

                return;
            }
    })
}
}
const page = (num: any) => {
    if (num < 1) {
        querinfo.index = 1
        return;
    }
    if (num > data.totalpage) {
        querinfo.index = data.totalpage
        return;
    }
    querinfo.index = num;
    lists();

}

</script>

<style scoped>
span {
    margin: 0 10px;
    
}
input{margin: 0 2px;}
</style>